﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cklib;
using Silmoon.Threading;
using Silmoon.Configure;
using Silmoon.Windows.Controls.Extension;

namespace Cooky.Client
{
    public partial class LoginForm : Silmoon.Windows.Forms.GenieForm
    {
        cklib.CookyDNSAPI cooky;
        GenieExtension ge = null;

        public LoginForm(cklib.CookyDNSAPI cooky)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            this.cooky = cooky;
            InitializeComponent();
            ge = new GenieExtension(this);
            CloseStyle = WindowCloseStyle.MaxStyleExt;

            IniFile ini = new IniFile(Application.StartupPath + "/conf.private");
            Silmoon.Security.CSEncrypt cse = new Silmoon.Security.CSEncrypt("cooky");
            ctlUsernameTextBox.Text = cse.Decrypt(ini.ReadInivalue("config", "username"));
            ctlPasswordTextBox.Text = cse.Decrypt(ini.ReadInivalue("config", "password"));
            if (ctlUsernameTextBox.Text != "") switchButton1.SwitchState = Silmoon.Windows.Controls.SwitchButton.SwitchStateType.On;
            cse.Dispose();
        }

        private void ctlCancelButton_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            Close();
        }

        private void ctlLoginButton_Click(object sender, EventArgs e)
        {
            ctlUsernameTextBox.Enabled = false;
            ctlPasswordTextBox.Enabled = false;
            ctlLoginButton.Enabled = false;
            Threads.ExecAsync(loginThread, null);
        }
        void loginThread()
        {
            UserResult result = cooky.Login(ctlUsernameTextBox.Text, ctlPasswordTextBox.Text);
            if (result.Success)
            {
                IniFile ini = new IniFile(Application.StartupPath + "/conf.private");
                if (switchButton1.SwitchState == Silmoon.Windows.Controls.SwitchButton.SwitchStateType.On)
                {
                    Silmoon.Security.CSEncrypt cse = new Silmoon.Security.CSEncrypt("cooky");
                    ini.WriteInivalue("config", "username", cse.Encrypt(ctlUsernameTextBox.Text));
                    ini.WriteInivalue("config", "password", cse.Encrypt(ctlPasswordTextBox.Text));
                    cse.Dispose();
                }
                else
                {
                    ini.WriteInivalue("config", "username", "");
                    ini.WriteInivalue("config", "password", "");
                }

                panel1.Visible = false;
                this.Invoke(new tmpHander(Close));
            }
            else
            {
                ctlUsernameTextBox.Enabled = true;
                ctlPasswordTextBox.Enabled = true;
                ctlLoginButton.Enabled = true;

                MessageBox.Show("用户名密码错误。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoginForm_Shown(object sender, EventArgs e)
        {
            //ge.ShowControl(label1, true);
            //ge.ShowControl(label2, true);
            //ge.ShowControl(label3, true);
            //ge.ShowControl(ctlPasswordTextBox, true);
            //ge.ShowControl(ctlUsernameTextBox, true);
            ge.ShowControl(ctlCancelButton, true, 0);
            ge.ShowControl(ctlLoginButton, true, 300);
            //ge.ShowControl(switchButton1, true);
        }
    }
    delegate void tmpHander();
}
